<?php

namespace bank;

use bank\BankUtils;
use account\Account;
use account\Deposit;
use account\Credit;
use client\Client;
use Exception;

class Bank {
	private $name;
	private $adress;
	private $products;
	private $balance;
	public function __construct($name, $adress, $products, $balance) {
		$this->setName ( $name );
		$this->setAdress ( $adress );
		$this->setProducts ( $products );
		$this->setBalance ( $balance );
	}
	public function __destruct() {
	}
	public function setName($name) {
		if (! is_string ( $name )) {
			throw new Exception ( 'Bank name must be string.' );
		}
		
		$this->name = $name;
	}
	public function getName() {
		return $this->name;
	}
	public function setAdress($adress) {
		if (! is_string ( $adress )) {
			throw new Exception ( 'Bank adress must be string.' );
		}
		if (trim ( strlen ( $adress ) ) < 6) {
			throw new Exception ( 'Bank adress is too short.' );
		}
		$this->adress = $adress;
	}
	public function getAdress() {
		return $this->adress;
	}
	public function setProducts($products) {
		$this->products = $products;
	}
	public function getProducts() {
		return $this->products;
	}
	public function setBalance($balance) {
		if (is_string ( $balance )) {
			throw new Exception ( 'Bank balance can not be string.' );
		}
		if (! is_numeric ( $balance )) {
			throw new Exception ( 'Bank balance must be numeric' );
		}
		
		$depositsSum = 0;
		
		foreach ( $this->products as $product ) {
			if (get_class ( $product ) == "Deposit") {
				$depositsSum += $product->getBalance ();
			}
		}
		
		if ($balance <= ($depositsSum / 10)) {
			throw new Exception ( "Bank balance is not big enought." );
		}
		$this->balance = $balance;
	}
	public function getBalance() {
		return $this->balance;
	}
	public function payDeposit(Deposit $deposit) {
		$deposit->setBalance ( $deposit->getBalance () + $deposit->getSum () );
		$this->setBalance ( $this->balance - $deposit->getSum () );
	}
	public function payDeposits() {
		foreach ( $this->getProducts () as $product ) {
			if (get_class ( $product ) == "Deposit") {
				$this->payDeposit ( $product );
			}
		}
	}
	public function paidCredit(Client $client, Credit $credit) {
		if ($credit->getBalance () == 0) {
			echo '<p>The credit is paid</p>';
			return false;
		}
		$credit->setBalance ( $credit->getBalance - $credit->getSum );
		$this->setBalance ( $this->balance + $credit->getSum );
	}
	public function giveCredit(Client $client, Credit $credit) {
		$creditsSum = 0;
		
		foreach ( $client->getCredits () as $c ) {
			$creditsSum += $c->getSum ();
		}
		
		if ($creditsSum + $credit->getSum () >= ($client->getSalary () / 2)) {
			echo '<p>' . $client->getName () . ' does not have enought money for another credit</p>';
			return false;
		}
		
		$depositsSum = 0;
		
		foreach ( $this->products as $product ) {
			if (get_class ( $product ) == "Deposit") {
				$depositsSum += $product->getBalance ();
			}
		}
		// echo $depositsSum;
		// $depositsSum = 0 !!!!!!!!!!!!!!!!
		if ($credit->getBalance () > $this->balance / 10) {
			echo "<p>Credit is too big for the bank.</p>";
		}
		
		$this->products [] = $credit;
		$this->balance -= ($credit->getBalance ());
		return true;
	}
	public function takeDeposit(Deposit $deposit) {
		$this->products [] = $deposit;
		$this->balance += ($deposit->getBalance ());
	}
	public function __toString() {
		$result = '<p>Bank name: ' . $this->getName () . '</p>
		<p>Bank adress: ' . $this->getAdress () . '</p>
		<p>Bank balance: ' . $this->getBalance () . '</p>
		<p>Products:';
		foreach ( $this->getProducts () as $product ) {
			$result .= '<p>Product type: ' . substr(get_class ( $product ), 8) . '</p>
			<p>Product name: ' . $product->getName () . '</p>
			<p>Product percent: ' . $product->getPercent () . '</p>
			<p>Product period: ' . $product->getPeriod () . '</p>
			<p>Product balance: ' . $product->getBalance () . '</p>
			<p>Product sum: ' . $product->getSum() .'</p><br/>';
		}
		$result .= '</p><br/>';
		return $result;
	}
	
}